<template>
	<a-drawer
		:title="formData.categoryId ? '编辑应用分类管理' : '增加应用分类管理'"
		:width="600"
		:visible="visible"
		:destroy-on-close="true"
		:footer-style="{ textAlign: 'right' }"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="分类名称：" name="categoryName">
				<a-input v-model:value="formData.categoryName" placeholder="请输入分类名称" allow-clear/>
			</a-form-item>
			<a-form-item label="排序编码：" name="sortCode">
				<a-input v-model:value="formData.sortCode" placeholder="请输入排序编码" allow-clear/>
			</a-form-item>
			<a-form-item label="删除标志：" name="deletionFlag">
				<a-input v-model:value="formData.deletionFlag" placeholder="请输入删除标志" allow-clear/>
			</a-form-item>
			<a-form-item label="创建时间：" name="creationTime">
				<a-date-picker v-model:value="formData.creationTime" value-format="YYYY-MM-DD HH:mm:ss" show-time placeholder="请选择创建时间" style="width: 100%"/>
			</a-form-item>
			<a-form-item label="创建人：" name="creator">
				<a-input v-model:value="formData.creator" placeholder="请输入创建人" allow-clear/>
			</a-form-item>
			<a-form-item label="更新人：" name="updater">
				<a-input v-model:value="formData.updater" placeholder="请输入更新人" allow-clear/>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</a-drawer>
</template>

<script setup name="appCategoryForm">
import {cloneDeep} from 'lodash-es'
import {required} from '@/utils/formRules'
import appCategoryApi from '@/api/biz/appCategoryApi'
// 抽屉状态
const visible = ref(false)
const emit = defineEmits({successful: null})
const formRef = ref()
// 表单数据
const formData = ref({})
const submitLoading = ref(false)

// 打开抽屉
const onOpen = (record) => {
	visible.value = true
	if (record) {
		let recordData = cloneDeep(record)
		formData.value = Object.assign({}, recordData)
	}
}
// 关闭抽屉
const onClose = () => {
	formRef.value.resetFields()
	formData.value = {}
	visible.value = false
}
// 默认要校验的
const formRules = {}
// 验证并提交数据
const onSubmit = () => {
	formRef.value
		.validate()
		.then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			appCategoryApi
				.appCategorySubmitForm(formDataParam, !formDataParam.categoryId)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
}
// 抛出函数
defineExpose({
	onOpen
})
</script>
